<?php
$autoload='external';

function stats_sort($array, $sortby, $sort)
{
	$zmiana=1;
	$x=0;

	while($zmiana==1)
	{
		$zmiana=0;

		for($x=0;$x<count($array)-1;$x++)
		{

			if($sort=='DESC')
			{
				if($array[$x][$sortby] < $array[$x+1][$sortby])
				{
					$tmp=$array[$x];
					$array[$x]=$array[$x+1];
					$array[$x+1]=$tmp;
					$zmiana=1;
				}
			}
			else
			{
				if($array[$x][$sortby] > $array[$x+1][$sortby])
				{
					$tmp=$array[$x+1];
					$array[$x+1]=$array[$x];
					$array[$x]=$tmp;
					$zmiana=1;
				}
			}
		}
	}

	return $array;
}
//

function show_txt_stats(){
	global $conf, $lang;

	if(!perms_check('admin', 'stats')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$fields=array('date', 'uni', 'raw');

	//sort
	if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
	$_GET['sort']='DESC';
	if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
	$_GET['sortby']='date';
	if($_GET['sort']=='ASC')
	$sortnext='DESC';
	else
	$sortnext='ASC';
	//

	//konfiguracja statystyk
	$days_pl=array('Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota');
	$days_en=array('Sunday', 'Monday', 'Tuesday', 'Wendesday', 'Thursday', 'Friday', 'Saturday');

	$months_pl=array('Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Paćdziernik','Listopad','Grudzień');
	$months_en=array('January', 'February','March','April','May','June','July','August','September','October','November','December');
	//

	if(!$_POST['from'] && $_GET['from'])
	$_POST['from']=$_GET['from'];

	if(!$_POST['to'] && $_GET['to'])
	$_POST['to']=$_GET['to'];

	$content='
<HTML>
<HEAD>
<LINK REL="stylesheet" HREF="'.$GLOBALS['theme_path'].'css.css" TYPE="text/css">
</HEAD>
<BODY TOPMARGIN="0" LEFTMARGIN="0" RIGHTMARGIN="0" BOTTOMMARGIN="0">
<TABLE BORDER="0" HEIGHT="100%" WIDTH="100%" CLASS="tabelka2">';  

	$db = new dbquery;
	$db2 = new dbquery;

	if($_POST['from'] or $_POST['to']) {
		$fr=$_POST['from'];
		$to=$_POST['to'];

		if($_POST['from'] && $_POST['to'])
		$ft="WHERE `date` >= '$fr' && date <= '$to'";
		elseif($_POST['from'])
		$ft="WHERE `date` >= '$fr'";
		elseif($_POST['to'])
		$ft="WHERE `date` <= '$to'";
	}

	$uni_all=0;
	$raw_all=0;

	$middle='';
	$db->query("SELECT * FROM $conf[prefix]stats $ft ORDER by $_GET[sortby] $_GET[sort]") or $db->err(__FILE__, __LINE__);
	while($d=$db->fetch_object())
	{
		$i++;
		if($i>2)
		$i=1;

		if($_SESSION['lang_short']=='pl')
		$day=$days_pl[date('w', strtotime($d->date))];
		else
		$day=$days_en[date('w', strtotime($d->date))];

		$middle.=$day.'	'.date('d-m-Y', strtotime($d->date)).'	'.$d->uni.'	'.$d->raw."\n";

		$uni_all+=$d->uni;
		$raw_all+=$d->raw;
	}

	$content.='<TR>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><TEXTAREA COLS="75" ROWS="20" CLASS="button">'.$middle.'</TEXTAREA></TD>
</TR>
<TR VALIGN="TOP">
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><INPUT TYPE="BUTTON" OnClick="javascript:window.close();" VALUE="'.$lang['close_window'].'" CLASS="button"></TD>
</TR>
</TABLE></BODY></HTML>';

	echo($content);
}

function show_standard_deviation(){
	global $conf, $lang;

	if(!perms_check('admin', 'stats')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$fields=array('date', 'uni', 'raw');

	//sort
	if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
	$_GET['sort']='DESC';
	if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
	$_GET['sortby']='date';
	if($_GET['sort']=='ASC')
	$sortnext='DESC';
	else
	$sortnext='ASC';
	//

	//konfiguracja statystyk
	$days_pl=array('Niedziela', 'Poniedzia�ek', 'Wtorek', '�roda', 'Czwartek', 'Pi�tek', 'Sobota');
	$days_en=array('Sunday', 'Monday', 'Tuesday', 'Wendesday', 'Thursday', 'Friday', 'Saturday');

	$months_pl=array('Stycze�','Luty','Marzec','Kwiecie�','Maj','Czerwiec','Lipiec','Sierpie�','Wrzesie�','Pa�dziernik','Listopad','Grudzie�');
	$months_en=array('January', 'February','March','April','May','June','July','August','September','October','November','December');
	//

	if(!$_POST['from'] && $_GET['from'])
	$_POST['from']=$_GET['from'];

	if(!$_POST['to'] && $_GET['to'])
	$_POST['to']=$_GET['to'];

	$content='
<HTML>
<HEAD>
<LINK REL="stylesheet" HREF="'.$GLOBALS['theme_path'].'css.css" TYPE="text/css">
</HEAD>
<BODY TOPMARGIN="0" LEFTMARGIN="0" RIGHTMARGIN="0" BOTTOMMARGIN="0" CLASS="tabelka2">
<TABLE ALIGN="CENTER" VALIGN="MIDDLE" WIDTH="75%" BORDER="0" CLASS="tabelka2">
<TR>
	 <TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=date&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">'.$lang['admin_date'].'</A></TD>
	 <TD CLASS="row_3" ALIGN="CENTER" COLSPAN="2"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=uni&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">UNI:</A></TD>
	 <TD CLASS="row_3" ALIGN="CENTER" COLSPAN="2"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=raw&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">RAW:</A></TD>
</TR>
<TR>
	 <TD CLASS="row_3" ALIGN="CENTER">-</TD>
	 <TD CLASS="row_3" ALIGN="CENTER">avarage</TD>
	 <TD CLASS="row_3" ALIGN="CENTER">standard<BR>deviation</TD>
	 <TD CLASS="row_3" ALIGN="CENTER">avarage</TD>
	 <TD CLASS="row_3" ALIGN="CENTER">standard<BR>deviation</TD>
</TR>';  

	$db = new dbquery;
	$db2 = new dbquery;

	if($_POST['from'] or $_POST['to']) {
		$fr=$_POST['from'];
		$to=$_POST['to'];

		if($_POST['from'] && $_POST['to'])
		$ft="WHERE `date` >= '$fr' && date <= '$to'";
		elseif($_POST['from'])
		$ft="WHERE `date` >= '$fr'";
		elseif($_POST['to'])
		$ft="WHERE `date` <= '$to'";
	}

	$uni_all=0;
	$raw_all=0;

	if($_SESSION['lang_short']=='pl')
	$array=$days_pl;
	else
	$array=$days_en;

	for($i=0;$i<7;$i++){
		$arrayw[$i]=array();
	}

	$arrayw=array();
	$c=0;
	$db->query("SELECT * FROM $conf[prefix]stats $ft ORDER by $_GET[sortby] $_GET[sort]") or $db->err(__FILE__, __LINE__);
	while($d=$db->fetch_array())
	{
		$i++;
		if($i>2)
		$i=1;

		$number=date('w', strtotime($d['date']));
		if(!$quantity[$number])
		$quantity[$number]=0;
		$arrayw[$number][$quantity[$number]]['uni']=$d['uni'];
		$arrayw[$number][$quantity[$number]]['raw']=$d['raw'];
		$sumu[$number]+=$d['uni'];
		$sumr[$number]+=$d['raw'];
		$quantity[$number]+=1;
	}

	for($i=0;$i<7;$i++){
		if(!$arrayw[$i])
		break;
		$sru[$i]=round($sumu[$i]/$quantity[$i],0);
		$srr[$i]=round($sumr[$i]/$quantity[$i]);
		for($x=0;$x<$quantity[$i];$x++) {
			$su[$i]+=($arrayw[$i][$x]['uni']-$sru[$i])*($arrayw[$i][$x]['uni']-$sru[$i]);
			$sr[$i]+=($arrayw[$i][$x]['raw']-$srr[$i])*($arrayw[$i][$x]['raw']-$srr[$i]);
		}
		$su[$i]=round(sqrt($su[$i]/($quantity[$i]-1)), 0);
		$sr[$i]=round(sqrt($sr[$i]/($quantity[$i]-1)), 0);
	}

	$i=0;
	for($x=0;$x<7;$x++){
		$i++;
		if($i>2)
		$i=1;
		if(!$arrayw[$i])
		break;
		$content.='<TR>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'">'.$array[$x].'</TD>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><B>'.$sru[$x].'</B></TD>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><I>'.$su[$x].'</I></TD>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><B>'.$srr[$x].'</B></TD>
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'"><I>'.$sr[$x].'</I></TD>
</TR>
';
	}

	$content.='<TR VALIGN="TOP">
	 <TD ALIGN="CENTER" CLASS="row_'.$i.'" COLSPAN="5"><INPUT TYPE="BUTTON" OnClick="javascript:window.close();" VALUE="'.$lang['close_window'].'" CLASS="button"></TD>
</TR>
</TABLE></BODY></HTML>';

	echo($content);
}

?>
